import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description: 数组中出现次数超过一半的数字
 * User: wangxin
 * Date: 2024-12-22
 * Time: 20:49
 */
public class Test1 {
    public int MoreThanHalfNum_Solution (int[] numbers) {
        // write code here
        Map<Integer,Integer> map = new HashMap<Integer,Integer>();
        for(int i = 0; i < numbers.length;i++){
            if(map.containsKey(numbers[i])){
                map.put(numbers[i],map.get(numbers[i])+1);
            }else{
                map.put(numbers[i],1);
            }
        }
        int n = numbers.length/2;

        for(Map.Entry<Integer,Integer> entry:map.entrySet()){
            if(entry.getValue() > n){
                return entry.getKey();
            }
        }
        return -1;
    }
}
